﻿using System;
using System.Collections.Generic;
using System.Text;
using HardwareLibrary.ComLibrary;
using System.IO.Ports;
using System.Threading;

namespace HardwareLibrary.IDReaderLibrary
{
    public class IDReaderHelper : ComHelper
    {
        string NowReceiverd = "";
        public IDRead_Struct IR = new IDRead_Struct();
        public bool OpenIDPort(IDRead_Struct idRead)
        {
            return OpenIDPort(idRead.iPort, idRead.iBaudRate, idRead.iBeginFlag, idRead.iEndFlag, idRead.strLen, idRead.BeginSite, idRead.IDReaderIDLen, idRead.IDCardBeginSite, idRead.IDCardDataLen);
        }
        public bool OpenIDPortByParam(IDRead_Struct idRead)
        {
            return OpenIDPort(idRead.iPort, idRead.strParam, idRead.iBeginFlag, idRead.iEndFlag, idRead.strLen, idRead.BeginSite, idRead.IDReaderIDLen, idRead.IDCardBeginSite, idRead.IDCardDataLen);
        }
        public bool OpenIDPort(int iPortNum, int iBaudRate, int iBeginFlag, int iEndFlag, int iStrLen, int iReaderBeginSite, int iReaderIDLen, int iIDCardBeginSite, int iIDCardDataLen)
        {
            bool RtnFlag = false;
            IR.BeginFlag = Chr(iBeginFlag);
            IR.EndFlag = Chr(iEndFlag);
            IR.strLen = iStrLen;
            IR.IDCardBeginSite = iIDCardBeginSite;
            IR.IDCardDataLen = iIDCardDataLen;
            IR.BeginSite = iReaderBeginSite;
            IR.IDReaderIDLen = iReaderIDLen;
            RtnFlag = base.OpenComPortWithEventHandler(iPortNum, iBaudRate);
            return RtnFlag;
        }
        public bool OpenIDPort(int iPortNum, string strParam, int iBeginFlag, int iEndFlag, int iStrLen, int iReaderBeginSite, int iReaderIDLen, int iIDCardBeginSite, int iIDCardDataLen)
        {
            bool RtnFlag = false;
            IR.BeginFlag = Chr(iBeginFlag);
            IR.EndFlag = Chr(iEndFlag);
            IR.strLen = iStrLen;
            IR.IDCardBeginSite = iIDCardBeginSite;
            IR.IDCardDataLen = iIDCardDataLen;
            IR.BeginSite = iReaderBeginSite;
            IR.IDReaderIDLen = iReaderIDLen;
            RtnFlag = base.OpenComPortWithEventHandler(iPortNum, strParam);
            return RtnFlag;
        }
        public bool OpenIDPortWithWatch(int iPortNum, string strParam, int iBeginFlag, int iEndFlag, int iStrLen, int iReaderBeginSite, int iReaderIDLen, int iIDCardBeginSite, int iIDCardDataLen)
        {
            bool RtnFlag = false;
            IR.BeginFlag = Chr(iBeginFlag);
            IR.EndFlag = Chr(iEndFlag);
            IR.strLen = iStrLen;
            IR.IDCardBeginSite = iIDCardBeginSite;
            IR.IDCardDataLen = iIDCardDataLen;
            IR.BeginSite = iReaderBeginSite;
            IR.IDReaderIDLen = iReaderIDLen;
            RtnFlag = base.OpenComPortNoEvent(iPortNum, strParam);
            ThreadStart ts = new ThreadStart(ID_Watching);
            Thread td = new Thread(ts);
            td.Start();
            return RtnFlag;
        }
        public override void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string tmp;
            int beginIndex;
            base.Port_DataReceived(sender, e);
            if (NowReceiverd == "")//如果当前接收值为空，表示初次接收
            {
                beginIndex = base.whatRecieved.IndexOf(IR.BeginFlag);
                if (beginIndex >= 0)//如果截取的字符串存在起始标示符
                {
                    tmp = base.whatRecieved.Substring(beginIndex, IR.strLen);//截取规定的长度，在截取后判断末尾是否是正确的标示符
                    NowReceiverd = tmp;
                    if (NowReceiverd.Length >= IR.strLen)//如果截取长度满足
                    {
                        IR.IDReaderID = NowReceiverd.Substring(IR.BeginSite - 1, IR.IDReaderIDLen);
                        IR.IDCardID = NowReceiverd.Substring(IR.IDCardBeginSite - 1, IR.IDCardDataLen);
                        NowReceiverd = "";
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else//表示仍有数据未接收完成
            {
                NowReceiverd = NowReceiverd + base.whatRecieved;
                if (NowReceiverd.Length >= IR.strLen)//如果截取长度满足
                {
                    IR.IDReaderID = NowReceiverd.Substring(IR.BeginSite - 1, IR.IDReaderIDLen);
                    IR.IDCardID = NowReceiverd.Substring(IR.IDCardBeginSite - 1, IR.IDCardDataLen);
                    NowReceiverd = "";
                }
                else
                {
                    return;
                }
            }
        }
        public void ID_Watching()
        {
            
            while (base.sPort.IsOpen)
            {
                WatchingEvent();
            }
        }
        public virtual void WatchingEvent()
        {
            Thread.Sleep(500);
            base.whatRecieved = base.sPort.ReadExisting();
            int beginIndex = base.whatRecieved.IndexOf(IR.BeginFlag);
            if (beginIndex >= 0)//如果截取的字符串存在起始标示符
            {
                string tmp = base.whatRecieved.Substring(beginIndex, IR.strLen);//截取规定的长度，在截取后判断末尾是否是正确的标示符
                IR.IDReaderID = tmp.Substring(IR.BeginSite - 1, IR.IDReaderIDLen);
                IR.IDCardID = tmp.Substring(IR.IDCardBeginSite - 1, IR.IDCardDataLen);
            }
        }
        /// <summary>
        /// ASCII码转字符串
        /// </summary>
        /// <param name="asciiCode"></param>
        /// <returns></returns>
        private string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }
    }
}
